﻿using Mapster;
using Volo.Abp.Application.Services;
using Ln.Tool.Application.Contracts.IServices;
using Ln.Tool.Application.Contracts.Dtos;
using Ln.Tool.Domain.Managers;
using Ln.Tool.Domain.Shared.Dtos;
using Microsoft.AspNetCore.Mvc;

namespace Ln.Tool.Application.Services;

public class TemplateGenService(TemplateGenManager templateGenManager) : ApplicationService, ITemplateGenService
{
	/// <summary>
	/// 下载模块文件
	/// </summary>
	/// <returns></returns>
	public async Task<FileContentResult> CreateModuleAsync(TemplateGenCreateInputDto moduleCreateInputDto)
	{
		moduleCreateInputDto.SetNameReplace();
		var input = moduleCreateInputDto.Adapt<TemplateGenCreateDto>();
		var filePath = await templateGenManager.CreateTemplateAsync(input);
		////考虑从路径中获取
		//var fileContentType = MimeHelper.GetMimeMapping(Path.GetFileName(filePath));
		//设置附件下载，下载名称
		FileContentResult result = new FileContentResult(await File.ReadAllBytesAsync(filePath), "application/octet-stream");
		result.FileDownloadName = $"{moduleCreateInputDto.Name}.zip";
		return await Task.FromResult(result);
	}
}
